//
//  CXTX_TabBarControl.m
//  UILibOCSample
//
//  Created by apple on 2018/3/15.
//  Copyright © 2018年 DJI. All rights reserved.
//

#import "CXTX_TabBarControl.h"
//#import "TestVC.h"
#import "FirstVC.h"
#import "SecondVC.h"
#import "ThirdVC.h"
#import "FourthVC.h"
#import "FifthVC.h"
#import "ToolView.h"
#import "PrepareFlyVC.h"
#import "DJIRootViewController.h"
//完全自定义tabbar
#import "MGBS_TabBar.h"
@interface CXTX_TabBarControl ()<MGBS_TabBarDelegate>
@property (nonatomic, strong) MGBS_TabBar *mgbs_tabBar;
@end

@implementation CXTX_TabBarControl
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
}

- (instancetype)init{
    if (self=[super init]) {
        //初始化
        // 创建子控制器
        [self addControllerWithClass:[FirstVC class] title:@"田" image:@"tab bar_icon_cropland" selectedImage:@"tab bar_icon_cropland_pre" withTabBar:self.mgbs_tabBar];
        [self addControllerWithClass:[SecondVC class] title:@"圈子" image:@"tab bar_icon_circle" selectedImage:@"tab bar_icon_circle_pre" withTabBar:self.mgbs_tabBar];
        [self addControllerWithClass:[ThirdVC class] title:@"任务" image:@"tab bar_icon_ task" selectedImage:@"tab bar_icon_circle_pre" withTabBar:self.mgbs_tabBar];
        [self addControllerWithClass:[FourthVC class] title:@"资讯" image:@"tab bar_icon_cropland" selectedImage:@"tab bar_icon_information_pre" withTabBar:self.mgbs_tabBar];
    }
    return self;
}

-(UIViewController *)addControllerWithClass:(Class)class title:(NSString *)title image:(NSString *)ImageName selectedImage:(NSString  *)selectImg withTabBar:(MGBS_TabBar*)tabBar{
    UIViewController *vc=[[class alloc]init];
    UINavigationController *nv=[[UINavigationController alloc]initWithRootViewController:vc];
    vc.tabBarItem.title=title;
    vc.tabBarItem.image=[UIImage imageNamed:ImageName];
    //选中时的图片
    UIImage *newImage=[UIImage imageNamed:selectImg];
    newImage=[newImage imageWithRenderingMode:YES];//图片原样显示
    vc.tabBarItem.selectedImage=newImage;
    
    
    // 设置tabBarButton的标题颜色的文字
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor]} forState:UIControlStateSelected];
//    [vc.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:169/255.0 green:108/255.0 blue:83/255.0 alpha:1.000]];
//    vc.navigationController.navigationBar.translucent = NO;
//#pragma mark -- 不加上这句，导航栏下面视图的y值从64开始计算
//    vc.extendedLayoutIncludesOpaqueBars = YES;
    [self addChildViewController:nv];
    // ********** 系统的tabbarItem已经被移除，这里我们需要把我们自定义的tabbar中加上一个tabbarItem，
    [tabBar addtabbarItem:vc.tabBarItem];
    
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor lightGrayColor];
    
    //创建自定义tabbar2
    MGBS_TabBar *tabbar=[[MGBS_TabBar alloc] init];//init创建控件
    self.mgbs_tabBar = tabbar;
    //设置大小
    self.mgbs_tabBar.frame= self.tabBar.frame;
    //添加到父控件中
    [self.view addSubview:self.mgbs_tabBar];

    //移除系统的tabBar
    self.tabBar.hidden = YES;
    [self.tabBar removeFromSuperview];
    //设置代理
    self.mgbs_tabBar.delegate = self;
}
/**
 *实现代理方法
 */

-(void)tabBar:(MGBS_TabBar *)tabBar selectBtnFrom:(NSInteger)from to:(NSInteger)to{
    //切换控制器
    UIViewController *vc = self.childViewControllers[to];
    self.selectedViewController = vc;
}

/**
 *中间按钮点击
 */
- (void)centerBtnClicked {
    @WeakObj(self);
    NSLog(@"中间按钮点击");
    ToolView *midView = [[ToolView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:midView];
    [self.view insertSubview:midView aboveSubview:self.mgbs_tabBar];
    midView.selectedBlock = ^(NSString *tag) {
        int i = [tag intValue];
        switch (i) {
                case 0:
            {
                //无人机
                PrepareFlyVC *vc = [PrepareFlyVC new];
                UINavigationController *nvi = [[UINavigationController alloc]initWithRootViewController:vc];
                [selfWeak presentViewController:nvi animated:YES completion:nil];
            }
                break;
                
            case 1:
            {
                //轨迹
                DJIRootViewController *vc = [DJIRootViewController new];
                UINavigationController *nvi = [[UINavigationController alloc]initWithRootViewController:vc];
                [selfWeak presentViewController:nvi animated:YES completion:nil];
            }
                break;
                
            case 2:
            {
//                //轨迹
//                TestVC *vc = [TestVC new];
//                UINavigationController *nvi = [[UINavigationController alloc]initWithRootViewController:vc];
//                [selfWeak presentViewController:nvi animated:YES completion:nil];
            }
                break;
                
            default:
                break;
        }
    };
}

-(void)dealloc {
    NSLog(@"tabbar控制器销毁");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
